<?php

namespace yiike\store\backend\controllers;
use yii\helpers\Url;
use yiike\order\model\Express;
use yiike\store\models\Order;
use yiike\store\models\OrderGoods;
use yiike\store\models\OrderGoodsSearch;
use yiike\store\models\OrderStore;

/**
* This is the class for controller "OrderController".
*/
class OrderController extends \yiike\store\backend\controllers\base\OrderController
{
    /**
     * Lists all OrderGoods models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel  = new OrderGoodsSearch();
        $dataProvider = $searchModel->search($_GET);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

    /**
     * 发货
     * @param $id
     * @throws \yii\web\HttpException
     */
    public function actionExpress($id) {
        $post = \Yii::$app->request->post('OrderStore');
        $express_code = $post['express_code'];
        $express_no = $post['express_no'];

        $express = Express::findOne($express_code);
        if (! $express_no) {
            return $this->message('请填写快递单号');
        }
        if (! $express) {
            return $this->message('物流公司不存在');
        }

        $model = $this->findModel($id);
        $model->status = OrderStore::$status_express;
        $model->express_name = $express->name;
        $model->express_code = $express->code;
        $model->express_no = $express_no;
        $model->express_at = date('Y-m-d H:i:s');
        if ($model->save()) {
            \Yii::$app->session->setFlash('发货成功');
            return $this->message('发货成功', 'success');
        }
        else {
            return $this->message('发货失败');
        }

        $this->goBack();
//        $this->redirect(Url::to(['view', 'id' => $id]));
    }

    public function actionRefund() {

    }

    public function actionReturn() {

    }
}
